<template>
  <div class="aaaa">
    <el-card>
      <div class="xxx">
        <div class="a">
          <i class="el-icon-info" />
          <span>共{{ xxxx }}条</span>
        </div>
        <div class="b">
          <el-button type="danger" size="small" @click="jiandan">简单表头导出</el-button>
          <el-button type="info" size="small">复杂表头导出</el-button>
          <el-button type="cuccess" size="small" @click="$router.push('/Yangon/excle')">exce批量导入</el-button>
          <el-button
            v-if="$store.state.user.userPro.roles.points.includes('employees_add_btn')"
            type="primary"
            size="small"
            @click="xinzen"
          >新曾员工</el-button>
        </div>
      </div>
    </el-card>

    <el-card style="margin-top: 30px">
      <el-table :data="list">
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            <div>
              {{ (a.page - 1) * a.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="username" label="姓名" />
        <el-table-column prop="staffPhoto" label="头像">
          <template v-slot="{ row }">
            <div>
              <img v-imgeroor="" style="width: 50px;" :src="row.staffPhoto || []" alt="" @click="yltx(row.staffPhoto)">
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" />
        <el-table-column prop="workNumber" label="工号" sortable />
        <el-table-column prop="formOfEmployment" label="聘用形式">
          <template #default="{ row }">
            <!-- {{ zh('hireType',row.formOfEmployment) }} -->
            <div v-fsdfs:hireType="row.formOfEmployment" />
          </template>
        </el-table-column>
        <el-table-column prop="departmentName" label="部门" />
        <el-table-column prop="timeOfEntry" label="入职时间" />
        <el-table-column prop="enableState" label="账户状态" />
        <el-table-column prop="date" label="操作" width="280">
          <template #default="{ row }">
            <div>
              <el-button type="text" @click="$router.push(`/Yangon/xianqin/${row.id}`), aa(row)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调用岗位</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="fpjs(row)">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: center; margin-top: 19px"
        :current-page.sync="a.page"
        :page-sizes="[1, 2, 10, 100, 200, 300, 400]"
        layout="total, sizes, prev, pager, next, jumper"
        :page-size.sync="a.size"
        :total="xxxx"
        @size-change="sizeChange"
        @current-change="getdata"
      />
    </el-card>

    <!-- 新增弹出框 -->
    <tck ref="tck" @getdata="getdata()" />
    <!-- 二维码 -->
    <yltxss ref="erwmtx" />

    <!-- 分配角色权限 -->
    <fenpeijues ref="fenpeijues" />
  </div>
</template>
<!--

  查
    1.定义api
    2.导入api
    3.进入页码调用api
    4.调用成功存储data数据中
    5.与分页器建立关联
        总数应用到分页器上
        页码页容量与接口参数中建立关联
        页码页容量发生变化，重新请求

  删
    1.点击按钮显示弹出框
    2.定义ipa
    3.导入api
    4.调用成功
        提示成功
        判断当前页容量是否大于1和当前页是否===1
        刷新数据
        关闭弹出框

   简单表头导出
     1. 拿到所有员工数据
     2. 导入excle导出方法
     3. 发送请求拿到员工数据，配好配置导出

    角色分配权限   特别
       1.封装弹出框
       2.导入注册
       3.点击角色打开弹出框
       4.完成内容布局(是请求接口来完成)
       5.回显数据
            a.如果当前能拿到数据 (做深拷贝)
            b.当前点击没有数据 ，发送请求
       6.定义导入api
       7.调用成功
            a.关闭弹出框
            b.清空表单，验证
            c.提示
            d.刷新数据（不在父级不用刷新）

-->

<script>
import { export_json_to_excel } from '@/vendor/Export2Excel'
import { sysuser, sysuserdelter } from '@/api/yuangon'
import jsCookie from 'js-cookie'
import tck from './components/indvue'
import { zh } from '@/api/constant/employees'

export default {
  components: {
    tck,
    yltxss: () => import('./components/erweim.vue'),
    fenpeijues: () => import('./components/fenpeijues.vue')
  },
  data() {
    return {
      list: [],
      a: {
        page: 1,
        size: +jsCookie.get('yrl') || 10
      },
      xxxx: 100
    }
  },
  created() {
    this.getdata()
  },
  methods: {

    // 分配角色权限
    async fpjs(row) {
      this.$refs.fenpeijues.show = true
      this.$refs.fenpeijues.huixian(row.id)
    },
    // 预览头像二维码
    yltx(x) {
      this.$refs.erwmtx.show = true

      this.$nextTick(() =>
        // 调用该方法，传值渲染二维码
        this.$refs.erwmtx.ffff(x)
      )
    },
    aa(a) {
      console.log(a)
    },
    async getdata() {
      const res = await sysuser(this.a)
      this.list = res.data.data.rows
      this.xxxx = res.data.data.total
    },
    // 聘用筛选
    formatters(row, column, Value, index) {
      // return zh('hireType', Value)
    },
    // 页容量持久化
    sizeChange() {
      jsCookie.set('yrl', this.a.size)

      this.getdata()
    },
    xinzen() {
      this.$refs.tck.show = true
    },

    // 删除
    async del(row) {
      if (row.username === '管理员') {
        this.$message.error('管理员不能删除')
      } else {
        await this.$confirm('确认删除吗')
        await sysuserdelter(row.id)
        await this.$message.success('删除成功')
      }
    },

    // 简单表头导出
    async jiandan() {
      const res = await sysuser({
        page: 1,
        size: 9999999
      })

      const res2 = res.data.data.rows.map(v => {
        return [
          v.username,
          v.staffPhoto,
          v.mobile,
          v.workNumber,
          zh('hireType', v.enableState),
          v.departmentName,
          v.correctionTime,
          v.formOfEmployment
        ]
      })
      console.log(res2)
      export_json_to_excel({
        header: ['姓名', '头像', '手机号', '员工', '聘用形式', '部门', '入职时间', '账户状态'],
        data: res2,
        filename: '员工信息'
      })
    }
  }
}
</script>

<style scoped lang="scss">
.aaaa {
  padding: 20px;

  .xxx {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .a {
      i {
        margin-right: 8px;
      }

      background-color: #f0f4fb;
      border: 1px solid #0964f7;
      padding: 7px;
      border-radius: 5px;
    }
  }
}
</style>
